/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;

public class Script extends ChildbearingPageElement implements IInlineElement, ICoreAttributes {

	private static final String		CONTENT_KEY		= "content";
	private static final String		CHARSET			= "charset";
	private static final String		SCRIPT			= "script";
	private static final String		TYPE			= "type";
	private static final Content	CDATA_TOP		= new Content("//<![CDATA[\r\n", false);
	private static final Content	CDATA_BOTTOM	= new Content("\r\n//]]", false);

	{
		this.setTag(SCRIPT);
		this.setSupportsAttributes(true);

	}
	private boolean					autoAppendContext;
	private Content					myContent;

	public Script() {
	}

	public Script(final boolean _autoAppendContext) {
		this();
		this.autoAppendContext = _autoAppendContext;
	}

	/**
	 * @throws UnsupportedOperationException since a script should not have children added directly.
	 */
	@Override
	public Script addPageElement(final PageElement _element) throws UnsupportedOperationException {
		throw new UnsupportedOperationException("Does not support others adding children.");
	}

	public String getCharset() {
		return this.getAttributeValue(CHARSET);
	}

	public String getDefer() {
		return this.getAttributeValue("defer");
	}

	public String getSrc() {
		return this.getAttributeValue("src");
	}

	public String getScriptBody() {
		return this.myContent == null ? null : this.myContent.getContent();
	}

	public String getType() {
		return this.getAttributeValue(TYPE);
	}

	public boolean isPreservingWhiteSpace() {
		return this.getAttributeValue("xml:space") != null;
	}

	/**
	 * 
	 * @return the script is expecting to have the App Context appended to is src attribute value.
	 */
	public boolean isAutoAppendContext() {
		return this.autoAppendContext;
	}

	/**
	 * Indicates if the tag should append the AppContext to the src attribute during rendering.
	 * @param _autoAppendContext if true, the tag will render the app context.
	 */
	public Script setAutoAppendContext(final boolean _autoAppendContext) {
		this.autoAppendContext = _autoAppendContext;
		return this;
	}

	public Script setPreservingWhiteSpace(final boolean _value) {
		if (_value)
			return (Script) this.setAttribute("xml:space", "preserve");
		else {
			this.removeAttribute("xml:space");
			return this;
		}
	}

	public Script setCharset(final String _value) {
		return (Script) this.setAttribute(CHARSET, _value);
	}

	public Script setDefer(final String _value) {
		return (Script) this.setAttribute("defer", _value);
	}

	public Script setSrc(final String _value) {
		return (Script) this.setAttribute("src", _value);
	}

	public Script setScriptBody(final String _body) {
		if (this.myContent == null) {
			this.myContent = new Content(_body, false);
		}
		this.myContent.setContent(_body);
		return this;
	}

	public Script setType(final String _value) {
		return (Script) this.setAttribute(TYPE, _value);
	}

	@Override
	public void render() throws IOException {
		this.addBeforeRenderCallBack(new IPageElementCallback<PageElement>() {

			@Override
			public void execute(final PageElement _elementBeingLoaded) {
				Script.this.beforeRender();
			}
		});
		super.render();
	}

	@Override
	public String getCssClass() {
		return this.getAttributeValue(CSS_CLASS_KEY);
	}

	@Override
	public String getId() {
		return this.getAttributeValue(ID_KEY);
	}

	@Override
	public String getStyle() {
		return this.getAttributeValue(STYLE_KEY);
	}

	@Override
	public String getTitle() {
		return this.getAttributeValue(TITLE_KEY);
	}

	@Override
	public Script setCssClass(final String _value) {
		return (Script) this.setAttribute(CSS_CLASS_KEY, _value);
	}

	@Override
	public Script setId(final String _value) {
		return (Script) this.setAttribute(ID_KEY, _value);
	}

	@Override
	public Script setStyle(final String _value) {
		return (Script) this.setAttribute(STYLE_KEY, _value);
	}

	@Override
	public Script setTitle(final String _value) {
		return (Script) this.setAttribute(TITLE_KEY, _value);
	}

	private void beforeRender() {
		if (this.autoAppendContext) {
			final String newSrc = "/" + this.getContextPath() + this.getSrc();
			this.setSrc(newSrc);
		}
		if (StringUtils.isEmpty(this.getSrc())) {
			super.addPageElement(CDATA_TOP);
			this.myContent.setElementId(CONTENT_KEY);
			super.addPageElement(this.myContent);
			super.addPageElement(CDATA_BOTTOM);

		}
	}
}
